feat: allow port forward to deployment and service#2749
Conversation
8d0ba93 to
3dffec5
Compare
| const namespace = process.argv[2] || 'default'; | ||
| const deploymentName = process.argv[3] || 'demo-deployment'; | ||
| const localPort = parseInt(process.argv[4] || '8080', 10); | ||
| const remotePort = parseInt(process.argv[5] || '8080', 10); |
There was a problem hiding this comment.
| const namespace = process.argv[2] || 'default'; | |
| const deploymentName = process.argv[3] || 'demo-deployment'; | |
| const localPort = parseInt(process.argv[4] || '8080', 10); | |
| const remotePort = parseInt(process.argv[5] || '8080', 10); | |
| const namespace = process.argv[2] ?? 'default'; | |
| const deploymentName = process.argv[3] ?? 'demo-deployment'; | |
| const localPort = parseInt(process.argv[4] ?? '8080', 10); | |
| const remotePort = parseInt(process.argv[5] ?? '8080', 10); |
|
|
||
| // This creates a local server that forwards traffic to a deployment in Kubernetes | ||
| // by resolving the deployment to its first ready pod and port-forwarding to that pod. | ||
| // Usage: npx ts-node port-forward-deployment.ts [namespace] [deploymentName] [localPort] [remotePort] |
There was a problem hiding this comment.
I don't think we should promote using ts-node. I believe it's unmaintained at this point. There is tsx, but also Node can run TypeScript directly now.
There was a problem hiding this comment.
Today I learned! Thank you I didn't knew NodeJS is capable of running typescript direclty.
In case anyone else stumbles upon this comment: https://nodejs.org/en/learn/typescript/run-natively
|
|
||
| const forward = new k8s.PortForward(kc); | ||
|
|
||
| const namespace = process.argv[2] || 'default'; |
There was a problem hiding this comment.
Same comment on these lines about using ?? instead of ||.
There was a problem hiding this comment.
I don't necessarily disagree, but why ?? instead of ||?
There was a problem hiding this comment.
I think in general we should prefer ?? unless we explicitly want to default on all falsy values. In this case, process.argv[x] will be undefined if not set, so they should behave identically. I haven't benchmarked it, but I'd also expect ?? to be faster since it does slightly less work under the hood.
|
|
||
| // This creates a local server that forwards traffic to a service in Kubernetes | ||
| // by resolving the service to its first ready pod and port-forwarding to that pod. | ||
| // Usage: npx ts-node port-forward-service.ts [namespace] [serviceName] [localPort] [remotePort] |
There was a problem hiding this comment.
Same comment about ts-node here.
examples/port-forward-deployment.js
Outdated
| const namespace = process.argv[2] || 'default'; | ||
| const deploymentName = process.argv[3] || 'demo-deployment'; | ||
| const localPort = parseInt(process.argv[4] || '8080', 10); | ||
| const remotePort = parseInt(process.argv[5] || '8080', 10); |
src/test/integration/portForward.ts
Outdated
| .catch((error) => { | ||
| console.error('Deployment port forward error:', error.message); | ||
| socket.destroy(); | ||
| }); |
There was a problem hiding this comment.
Can we use async-await and try...catch to be more consistent.
src/test/integration/portForward.ts
Outdated
| }); | ||
|
|
||
| // Give the server a moment to start | ||
| await setTimeout(500); |
There was a problem hiding this comment.
Is this necessary? Once the listen() Promise above resolves, the server should be ready to serve traffic.
src/test/integration/portForward.ts
Outdated
| const serviceServer = net.createServer((socket) => { | ||
| portForward | ||
| .portForwardService(namespace, serviceName, [containerPort], socket, null, socket) | ||
| .catch((error) => { |
There was a problem hiding this comment.
Same comment here about using async-await and try...catch.
src/test/integration/portForward.ts
Outdated
| }); | ||
|
|
||
| // Give the server a moment to start | ||
| await setTimeout(500); |
There was a problem hiding this comment.
Same question about this being necessary.
src/test/integration/portForward.ts
Outdated
| // Test connection to service via port-forward | ||
| for (let i = 0; i < 5; i++) { | ||
| try { | ||
| const response = await new Promise<string>((resolve, reject) => { |
There was a problem hiding this comment.
Is this logic all the same as above? If so, maybe it should be placed in its own function.
|
@cjihrig thanks for your review. I think I've addressed all your comments. I've made them in separate commits to easier keep track of what I already did and what I still need to do so it also be easy to review. |
cjihrig
left a comment
There was a problem hiding this comment.
LGTM. Maybe we should give it another day for other reviewers to weigh in, but I'm fine with landing this.
| @@ -1,40 +1,40 @@ | |||
| import * as k8s from '@kubernetes/client-node'; | |||
| // import * as k8s from '@kubernetes/client-node'; | |||
| import * as k8s from '../../../dist/index.js'; | |||
There was a problem hiding this comment.
This looks like maybe it was left over from local testing.
There was a problem hiding this comment.
I've removed it, good catch.
Prettier autoformatted the file so I would leave it in here.
src/test/integration/portForward.ts
Outdated
| assert.strictEqual(podsReady, true, 'Deployment pods did not become ready in time'); | ||
|
|
||
| try { | ||
| // Test 1: Port forward to deployment |
There was a problem hiding this comment.
LGTM
can we move test 1, test2, test3 into named functions to organize main test func body?
deploymentSuccessTest(...)
serviceSuccessTest(...)
serviceNoSelectorTest(...)
src/test/integration/portForward.ts
Outdated
| // Wait for pods to be ready | ||
| console.log('Waiting for deployment pods to be ready...'); | ||
| let podsReady = false; | ||
| for (let i = 0; i < 60; i++) { |
There was a problem hiding this comment.
nit the cycles/timouts are easier to understand when they are extracted into const
| for (let i = 0; i < 60; i++) { | |
| const MAX_POD_READY_CHECKS = 60; | |
| const POD_READY_CHECK_POLL_DELAY_MS = 1000; | |
| for (let i = 0; i < MAX_POD_READY_CHECKS; i++) { |
src/test/integration/portForward.ts
Outdated
| console.log(`Deployment is ready with ${deployment.status?.readyReplicas} replicas`); | ||
| break; | ||
| } | ||
| await setTimeout(1000); |
There was a problem hiding this comment.
| await setTimeout(1000); | |
| await setTimeout(POD_READY_CHECK_POLL_DELAY_MS); |
src/test/integration/portForward.ts
Outdated
| } | ||
|
|
||
| async function testPortForwardConnection(testPort: number, label: string): Promise<boolean> { | ||
| for (let i = 0; i < 5; i++) { |
There was a problem hiding this comment.
nit similar magic number or comment here
davidgamero
left a comment
There was a problem hiding this comment.
LGTM just a couple nits
|
Thanks @davidgamero I've adjusted the tests as you mentioned to be more modular :) |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: cjihrig, mstruebing The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
fixes #2746